package com.ilop.siteraudio.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ilop.siteraudio.R;
import com.ilop.siteraudio.adapter.RecordListAdapter;
import com.ilop.siteraudio.data.bean.RecordBean;
import com.ilop.siteraudio.data.util.RecordDaoUtil;
import com.ilop.siteraudio.util.CustomToastUtil;
import com.ilop.siteraudio.util.FileUtils;
import com.ilop.siteraudio.util.StatusBarUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2/1/21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：报告列表
 */
public class ReportListActivity extends AppCompatActivity {

    private static final String TAG = ReportListActivity.class.getSimpleName();

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.report_list)
    RecyclerView mRecyclerView;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.iv_report_back)
    ImageView mReportListBack;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.report_select)
    TextView mReportSelect;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.layout_report_empty)
    LinearLayout mReportEmpty;

    private RecordListAdapter mAdapter;
    private boolean isSelected = false;
    private ArrayList<Integer> mRecordIdList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_list);
        StatusBarUtil.setDefaultStatus(this);
        ButterKnife.bind(this);
        initView();
        mReportListBack.setOnClickListener(view -> finish());
        mReportSelect.setOnClickListener(view -> {
            if (isSelected && mRecordIdList.size()>0){
                Intent intent = new Intent(ReportListActivity.this, SendReportActivity.class);
                intent.putIntegerArrayListExtra("recordIdList", mRecordIdList);
                startActivity(intent);
            }
            isSelected = !isSelected;
            mReportSelect.setText(isSelected ? R.string.send : R.string.select);
            mAdapter.showSelectBox(isSelected);
        });
    }

    private void initView() {
        mRecordIdList = new ArrayList<>();
        List<RecordBean> mList = RecordDaoUtil.getInstance().getRecordDao().queryAll();
        if (mList.size() > 0) {
            mReportEmpty.setVisibility(View.GONE);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mAdapter = new RecordListAdapter(this, new RecordListAdapter.onLocalFileItemClick() {
                @Override
                public void onRecordClick(long recordId) {
                    Intent intent = new Intent(ReportListActivity.this, ReportDetailActivity.class);
                    intent.putExtra("recordId", recordId);
                    startActivity(intent);
                }

                @Override
                public void onRecordLongClick(String path) {
                    Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
                    intent.putExtra(Intent.EXTRA_SUBJECT,"文件分享" );
                    // 兼容android 7.0+
                    File file = FileUtils.getFileByPath(path);
                    if (file == null) {
                        CustomToastUtil.showToast(ReportListActivity.this, getString(R.string.no_report));
                    }else {
                        Uri uri = FileProvider.getUriForFile(ReportListActivity.this, getPackageName() + ".provider", file);
                        intent.putExtra(Intent.EXTRA_STREAM, uri);
                        intent.setType("image/*");
                        startActivity(intent);
                    }
                }

                @Override
                public void onSelectReport(ArrayList<Integer> report) {
                    mRecordIdList.clear();
                    mRecordIdList.addAll(report);
                }
            });
            mAdapter.setList(mList);
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mReportEmpty.setVisibility(View.VISIBLE);
            mReportSelect.setVisibility(View.GONE);
        }
    }
}
